﻿using Screen_Capture.Applications.ViewModels;
using System.ComponentModel.Composition;
using System.Waf.Applications;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.IO;

namespace Screen_Capture.Applications.Controllers
{
    [Export]
    internal class ShellController
    {
        private readonly ShellViewModel shellViewModel;
        private readonly DelegateCommand screenCaptureCommand;

        [ImportingConstructor]
        public ShellController(ShellViewModel shellViewModel)
        {
            this.shellViewModel = shellViewModel;
            this.screenCaptureCommand = new DelegateCommand(ScreenCaptureCommand);
        }

        public void Initialize()
        {
            shellViewModel.ScreenCaptureCommand = this.screenCaptureCommand;
        }


        /// <summary>
        /// 截屏
        /// </summary>
        private void ScreenCaptureCommand()
        {
            string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string fileName = desktop + "\\test.jpg";
            CopyScreenToFile(fileName);
            MessageBox.Show("截屏图像保存到：\n" + fileName);
        }

        /// <summary>
        /// 保存屏幕图像到文件
        /// </summary>
        /// <param name="fileName"></param>
        private void CopyScreenToFile(string fileName)
        {
            using (var screenBmp = new Bitmap(
                (int)SystemParameters.PrimaryScreenWidth,
                (int)SystemParameters.PrimaryScreenHeight,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                using (var bmpGraphics = Graphics.FromImage(screenBmp))
                {
                    bmpGraphics.CopyFromScreen(0, 0, 0, 0, screenBmp.Size); // 全屏幕截屏
                    screenBmp.Save(fileName);
                }
            }
        }

    }
}
